<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\SmsController;
use App\Models\CollPosition;
use App\Models\CollArea;
use App\Models\CompanyUser;
use App\Models\News;
use App\Models\PersonalUser;
use App\Models\User;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class IndexController extends CommonController
{
    /**
     * 首页
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        //资讯
//        $news = News::select('id', 'title')->orderBy('updated_at', 'desc')->take(2)->get();
        $news = DB::table('company_job')
            //获取字段
            ->select('id', 'job_title')
            ->where('job_status', 1)
            ->orderBy('id', 'desc')
            ->take(10)->get();
        $resumes = $company_jobs = [];

        if (auth()->guard('company')->check()) {
            //企业登录
            $resumes = DB::table('resume')
                // ->join('resume_extra as t2', 't1.id', '=', 't2.resume_id')
                //获取字段
                ->select(
                    'id', 'uid', 'realname', 'sex', 'birthday', 'education', 'homeaddress', 'regtime',
                    'work_year', 'jobsort1', 'jobarea1', 'updatetime', 'avatar', 'resume_status'
                )
                ->where('resume_status', '>=', 3)
                ->orderBy('regtime', 'desc')
                ->skip($request->page)->take(10)->get();
            if ($resumes) {
                foreach ($resumes as $v1) {
                    $jobsort = ($v1->jobsort1) ? CollPosition::getPosName($v1->jobsort1) : '';
                    $v1->jobsort = $jobsort;
                    if (empty($v1->jobarea1)) {
                        $jobarea = '';
                    } else {
                        $jobarea = CollArea::getAreaName($v1->jobarea1);
                    }
                    $v1->jobarea = $jobarea;
                }
            }
        } else {
            $company_jobs = DB::table('company_job as t1')
                ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
                //获取字段
                ->select(
                    't1.id', 't1.com_id', 't1.job_title', 't1.job_category1', 't1.location1', 't1.address',
                    't1.recruiting_num', 't1.salary', 't1.addtime',
                    't2.company_name'
                )
                ->where('job_status', 1)
                ->orderBy('t1.addtime', 'desc')
                ->skip($request->page)->take(10)->get();

            if ($company_jobs) {
                foreach ($company_jobs as $v2) {
                    $location = CollArea::getAreaName($v2->location1);
                    $v2->location = $location . ' ' . $v2->address;
                    $v2->job_category1 = CollPosition::getPosName($v2->job_category1);
                }
            }
        }

        // 获取推荐热门工种
        $position = CollPosition::select('id', 'cid', 'bg_color', 'img1', 'name')->where('grade', 1)->orderBy('sort', 'asc')->get();
        if ($position) {
            foreach ($position as $v3) {
                if ($v3->img1) {
                    $v3->img1 = self::$baseImg . '/' . $v3->img1;
                }
            }
        }

        $data = [
            'news' => $news,
            'resumes' => $resumes,
            'company_jobs' => $company_jobs,
            'position' => $position
        ];
        return response()->json(['status' => 1, 'data' => $data]);
    }

    /**
     * 视频详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getVedioInfo(Request $request)
    {
        $id = $request->id;
        $info = Video::where('id', $id)->first();
        if (!$info) {
            return response()->json(['status' => 0, 'msg' => '该视频不存在！']);
        } else {
            return response()->json(['status' => 1, 'data' => $info]);
        }
    }

    /**
     * 是否收藏某视频
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function isCollectVideo(Request $request)
    {
        $uid = $request->uid;
        $videoId = $request->video_id;
        $cate = $request->cate;
        $collect = DB::table('collection_video')->where('uid', $uid)->where('video_id', $videoId)->where('type', $cate)->first();
        if ($collect) {
            $isCollect = 1;
        } else {
            $isCollect = 0;
        }

        return response()->json(['status' => 1, 'data' => $isCollect]);
    }

    /**
     * 收藏职位
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCollectVideo(Request $request)
    {
        $uid = $request->uid;
        $videoId = $request->video_id;
        $cate = $request->cate;
        $collect = DB::table('collection_video')->where('uid', $uid)->where('video_id', $videoId)->where('type', $cate)->first();
        if ($collect) {
            return response()->json(['status' => 2, 'msg' => '已收藏！']);
        } else {
            $data = [
                'uid' => $uid,
                'video_id' => $videoId,
                'type' => $cate,
                'regtime' => date('Y-m-d H:i:s', time()),
                'updatetime' => date('Y-m-d H:i:s', time()),
                'status' => 1
            ];
            DB::table('collection_video')->insert($data);
            return response()->json(['status' => 1, 'msg' => '收藏成功！']);
        }
    }

    /**
     * 取消收藏
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delCollectVideo(Request $request)
    {
        $uid = $request->uid;
        $videoId = $request->video_id;
        $cate = $request->cate;
        $collect = DB::table('collection_video')->where('uid', $uid)->where('video_id', $videoId)->where('type', $cate)->first();
        if (!$collect) {
            return response()->json(['status' => 2, 'msg' => '未收藏！']);
        } else {
            DB::table('collection_video')->where('id', $collect->id)->delete();
            return response()->json(['status' => 1, 'msg' => '取消成功！']);
        }
    }

    /**
     * 添加次数
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addVideoNum(Request $request)
    {
        $videoId = $request->video_id;
        $res = Video::where('id', $videoId)->increment('view_num', 1);
        if ($res) {
            return response()->json(['status' => 1, 'msg' => '增加成功！']);
        } else {
            return response()->json(['status' => 0, 'msg' => '增加失败！']);
        }
    }

    /**
     * 我收藏的视频
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function collectVideos(Request $request)
    {
        $totalList = 10;
        $page = $request->page;
        $start = ($page - 1) * $totalList;
        $uid = $request->uid;

        $company_job = DB::table('collection_job as t3')
            ->join('company_job as t1', 't1.id', '=', 't3.job_id')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            //获取字段
            ->select(
                't1.id', 't1.job_title', 't1.job_category1', 't1.location1', 't1.address', 't1.salary', 't1.description', 't1.welfare', 't1.updatetime',
                't2.company_name', 't2.logo'
            )
            ->where('t3.uid', $uid)
            ->orderBy('id', 'desc')
            ->skip($start)->take($totalList)->get();

        if (count($company_job) > 0) {
            foreach ($company_job as $k => $v) {
                //地区
                $v->location = CollArea::getAreaName($v->location1);

                if (trim($v->location) == '') {
                    $v->location = "地区不限";
                }
                if ($v->address) {
                    $v->location = $v->location . ' ' . $v->address;
                }

                $v->job_category1 = CollPosition::getPosName($v->job_category1);
            }
        }

        return response()->json(['status' => 1, 'data' => $company_job]);
    }

    /**
     * 意见反馈
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addOpinion(Request $request)
    {
        $uid = $request->uid;
        $cate = $request->cate;
        $content = $request->description;

        $data = [
            'uid' => $uid,
            'content' => $content,
            'type' => $cate,
            'created_at' => date('Y-m-d H:i:s', time()),
            'updated_at' => date('Y-m-d H:i:s', time()),
            'status' => 1
        ];
        $res = DB::table('opinions')->insert($data);
        if ($res) {
            return response()->json(['status' => 1, 'msg' => '提交成功！']);
        } else {
            return response()->json(['status' => 0, 'msg' => '提交失败！']);
        }
    }

    /**
     * 可用银行列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function banklist()
    {
        $lists = DB::table('bank_list')
            ->orderBy('id', 'desc')
            ->get();

        return response()->json(['status' => 1, 'data' => $lists]);
    }

    /**
     * 验证支付密码（通过短信验证码）
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkPwd2(Request $request)
    {
        $uid = $request->uid;
        $code = trim($request->code);
        $password = $request->password;
        if (!$password || !$code) {
            return response()->json(['status' => 0, 'msg' => '参数缺失！']);
        }
        $user = PersonalUser::where('uid', $uid)->select('mobile', 'password2')->first();

        $mInfo = DB::table('mobile_code')->where('mobile', $user->mobile)->where('code', $code)->first();
        if (!$mInfo) {
            return response()->json(['status' => 0, 'msg' => '短信验证失败！']);
        }

        if (!$user->password2) {
            return response()->json(['status' => 0, 'msg' => '该支付密码尚未设置！']);
        }
        $res = Hash::check($password, $user->password2);
        if ($res) {
            return response()->json(['status' => 1, 'msg' => '密码验证成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '原密码验证失败！']);
        }
    }

    /**
     * 直接验证支付密码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkPsd2(Request $request)
    {
        $uid = $request->uid;
        $password = $request->password;
        if (!$password) {
            return response()->json(['status' => 0, 'msg' => '参数缺失！']);
        }
        $user = PersonalUser::where('uid', $uid)->select('password2')->first();

        if (!$user->password2) {
            return response()->json(['status' => 0, 'msg' => '该支付密码尚未设置！']);
        }
        $res = Hash::check($password, $user->password2);
        if ($res) {
            return response()->json(['status' => 1, 'msg' => '密码验证成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '支付密码验证失败！']);
        }
    }

    /**
     * 设置或更新登录密码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changePwd1(Request $request)
    {
        $uid = $request->uid;
        $code = trim($request->code);
        $password = $request->password;
        if (!$password || !$code) {
            return response()->json(['status' => 0, 'msg' => '参数缺失！']);
        }

        $user = User::where('uid', $uid)->select('mobile', 'password')->first();

        $mInfo = DB::table('mobile_code')->where('mobile', $user->mobile)->where('code', $code)->first();
        if (!$mInfo) {
            return response()->json(['status' => 0, 'msg' => '短信验证失败！']);
        }

        if (Hash::check($password, $user->password)) {
            return response()->json(['status' => 0, 'msg' => '请重新输入密码！']);
        };

        $nePassword = Hash::make($password);

        $res = User::where('uid', $uid)->update(['password' => $nePassword]);

        if ($res) {
            return response()->json(['status' => 1, 'msg' => '密码更新成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '登录密码更新失败！']);
        }

    }

    /**
     * 设置或更新支付密码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changePwd2(Request $request)
    {
        $uid = $request->uid;
        $code = trim($request->code);
        $password = $request->password;
        $cate = $request->cate;
        if (!$password || !$code || !$cate) {
            return response()->json(['status' => 0, 'msg' => '参数缺失！']);
        }

        if ($cate == 1) {
            $user = PersonalUser::where('uid', $uid)->select('mobile', 'password2')->first();
        } elseif ($cate == 2) {
            $user = CompanyUser::where('uid', $uid)->select('mobile', 'password2')->first();
        }

        $mInfo = DB::table('mobile_code')->where('mobile', $user->mobile)->where('code', $code)->first();
        if (!$mInfo) {
            return response()->json(['status' => 0, 'msg' => '短信验证失败！']);
        }

        if (!$user->password2) {
            $msg = '设置';
        } else {
            $msg = '更新';
        }
        $nePassword = Hash::make($password);
        if ($cate == 1) {
            $res = PersonalUser::where('uid', $uid)->update(['password2' => $nePassword]);
        } elseif ($cate == 2) {
            $res = CompanyUser::where('uid', $uid)->update(['password2' => $nePassword]);
        }

        if ($res) {
            return response()->json(['status' => 1, 'msg' => '密码' . $msg . '成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '支付密码' . $msg . '失败！']);
        }
    }
}
